//////////////////////////////////////////////////////////////////////////////
//
//  Created by Philip Mulcahy on 08/01/2012.
//  Copyright (c) 2012 Philip Mulcahy. All rights reserved.
//
//  This file is part of the note-recog library.
//
//  note-recog is free software: you can redistribute it and/or modify
//  it under the terms of version 3 of the GNU Lesser General Public License 
//  as published by the Free Software Foundation.
//
//  note-recog is distributed is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with note-recog.  If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////

#include <vector>
#include <iostream>
#include "TestData.h"
#include <cassert>
#include <fstream>
//#import "dirent.h"

TestData TestData::s_instance;

const std::vector<short>& TestData::get( const char * name )
{
    const std::string key(name);
    std::map<
        std::string,
        const std::vector<short>* >::const_iterator it = s_instance._data.find(key);
    
    if( it == s_instance._data.end() )
    {
        s_instance._data[key] = read( name );
    }

    return *s_instance._data[key];
}

TestData::TestData()
{
}

const std::vector<short> * TestData::read( const char * source )
{
//    DIR *dir;
//    struct dirent *ent;
//    dir = opendir ("note-recog/testdata/");
//    
//    if (dir != NULL) {
//        
//        //        /* print all the files and directories within directory */
//        while ((ent = readdir (dir)) != NULL) {
//            printf ("########### %s\n", ent->d_name);
//        }
//        closedir (dir);
//    } else {
//        std::cout << "ERROR: couldn't open ./" << std::endl;
//        assert(0);
//    }
    
    std::vector<short> * const data = new std::vector<short>;
//    std::string path = "note-recog/testdata/";
    std::string path = "/Users/philip/dev/jane/note-recog/note-recog/note-recog/testdata/";
    path += source;
    std::ifstream ifs(path.c_str());
    while (ifs.good()) {
        const size_t buffsize = 1024;
        char buff[buffsize];
        char * const pbuff = &(buff[0]);
        ifs.getline(pbuff,buffsize);
        const short val = atoi(pbuff);
        data->push_back(val);
    }
    
    ifs.close();    
    
    const bool empty = data->empty();
    assert(!empty);
    return data;
}
